//*****************************************************************************
//  172x72 COG Display Temp. Comp. FW
//  MCU: MSP430F235
//  PCB: 10424
//  Compiler: TI Code Composer Essentials MSP430 v3.1
//  Kent Displays, Inc.
//  Copyright (c) 2009. All rights reserved worldwide.
//  Revisions
//     A - Initial. (24V Drive System, No Focal Conic Erase)
//     B - 28V Drive System, Focal Conic Erase
//*****************************************************************************

#include "defines.h"
#include "system.h"
#include "display.h"
#include "image.h"
#include "led.h"
#include "cal.h"
#include "GT22.h"
#include "handl.h" 
#include "hal_ucs.h"
#include "mystring.h"

// Wakeup source definitions.
typedef enum {RS232_WAKE, TIMER, SWITCH1} WakeupSources;

// Global Variables
volatile WakeupSources gWakeSource;    // Wakeup source (set by ISR's)

// Global variables.
volatile unsigned int gTimer;   // Used to extend HW 16 bit counter to 32 bits.

unsigned char Rx_Buffer = 0x00;

typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;

volatile TestStatus TransferStatus1 = FAILED;

TestStatus Buffercmp(unsigned char* pBuffer1, unsigned char* pBuffer2, unsigned int BufferLength);

volatile unsigned char rev_flag = 0;
/*
#define receive_length 5 
unsigned char shangpingming[8] = {0xc8, 0xb8, 0xb3, 0xb2, 0xbf, 0xa7, 0xb7, 0xc8};
unsigned char jiage[5] = {3, 7, 0x0a, 6, 8};
unsigned char tiaoxingma[13] = {6, 9, 1, 7, 8, 7, 8, 0, 0, 7, 9, 7, 7};
unsigned char chandi[4] = {0xd0, 0xec, 0xd6, 0xdd};
unsigned char guige[6] = {0xa3, 0xb1, 0xa3, 0xb4, 0xa3, 0xb3};
unsigned char denji[2] = {0xa3, 0xc1};
unsigned char danwei[2] = {0xba, 0xd0};
*/

unsigned char sum_buffer[300] = {0x00};
unsigned char buffer1[64] = {
	0x32, 0x64, 0x00, 0x00, 0x01, 0x33, 0x33, 0x71, 
	0x8F, 0x02, 0xa3, 0xc1, 0x89, 0x23, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x07, 0xFF, 0xF8, 0x1F, 0xFF,
	0x9F, 0xFF, 0xE0, 0x7F, 0xFF, 0x9F, 0x07, 0xFF,
	0xa4
};

unsigned char buffer2[64] = {
	0x34, 0x64, 0x00, 0x00, 0x01, 0x33, 0x33, 0x72, 
	0x8D, 0x04, 0xd0, 0xec, 0xd6, 0xdd, 0x89, 0x23, 0xFC,
	0x3F, 0xFF, 0x9F, 0xFF, 0xF1, 0xFF, 0xFF, 0xBF,
	0x07, 0xFF, 0xFC, 0x7F, 0xFF, 0x9F, 0xFF, 0xF3,
	0xFF, 0xFF, 0x3E, 0x0F, 0xC0, 0x7E, 0xFF, 0xFF,
	0x9F, 0x01, 0xFB, 0xFF, 0xFF, 0x3E, 0x0F, 0x80,
	0x3E, 0xF8, 0x07
};

unsigned char buffer3[64] = {
	0x36, 0x64, 0x00, 0x00, 0x01, 0x33, 0x33, 0x73, 
	0x8E, 0x06, 0xa3, 0xb1, 0xa3, 0xb4, 0xa3, 0xb3,
	0x89, 0x23, 0x00, 0x3E, 0x00, 0xFB, 0xE0, 0x00,
	0x3E, 0x0F, 0x80, 0x7E, 0xF8, 0x00, 0x3E, 0x00,
	0xFF, 0xE0, 0x00, 0x3E, 0x0F, 0x80, 0x7C, 0xF8,
	0x00, 0x3E, 0x01, 0xF7, 0xE0, 0x00, 0x7E, 0x0F,
	0xFF, 0xFD, 0xFF, 0xFF, 0x3E, 0xf6
};

unsigned char buffer4[64] = {
	0x32, 0x64, 0x00, 0x00, 0x01, 0x33, 0x33, 0x74, 
	0x9A, 0x02, 0xba, 0xd0, 0x89, 0x23, 0xFF, 0xF7, 0xFF,
	0xF8, 0x7C, 0x1F, 0xFF, 0xF9, 0xFF, 0xFE, 0x7F,
	0xFF, 0xE3, 0xFF, 0xFE, 0x7C, 0x1F, 0xFF, 0xF9,
	0xFF, 0xFE, 0x7F, 0xFF, 0xE3, 0xFF, 0xFE, 0x7C,
	0x1F, 0xFF, 0xF1, 0xFF, 0xFE, 0x7F, 0xFF, 0xC1,
	0x21
};

unsigned char buffer5[64] = {
	0x35, 0x64, 0x00, 0x00, 0x01, 0x33, 0x33, 0x75, 
	0x8A, 0x05, 0x00, 0x0A, 0x03, 0x03, 0x42, 0x89, 0x23, 
	0xFF, 0xFE, 0x7C, 0x1F, 0xFF, 0x81, 0xF0, 0x00,
	0x7F, 0xFE, 0x00, 0x00, 0x7E, 0xF8, 0x1F, 0x00,
	0x01, 0xF0, 0x00, 0x7C, 0x00, 0x00, 0x00, 0x3E,
	0xF8, 0x3E, 0x00, 0x03, 0xF0, 0x00, 0xFC, 0x00,
	0x00, 0x00, 0x3E, 0xde
};

unsigned char buffer6[64] = {
	0x3d, 0x64, 0x00, 0x00, 0x01, 0x33, 0x33, 0x76, 
	0x8B, 0x0D, 0x06, 0x09, 0x00, 0x02, 0x08, 0x02, 
	0x07, 0x02, 0x00, 0x03, 0x00, 0x01, 0x02, 0x89, 
	0x23, 0xF8, 0x3E, 0x00, 0x03, 0xFF, 0xFE, 0xF8,
	0x00, 0x0F, 0xFF, 0xFE, 0xF8, 0x3E, 0x00, 0x03,
	0xFF, 0xFE, 0xF8, 0x00, 0x0F, 0xFF, 0xFC, 0xF8,
	0x3E, 0x00, 0x01, 0xFF, 0xFC, 0xF8, 0x00, 0x0F,
	0xFF, 0xF9, 0xF0, 0x3E, 0x24
};

unsigned char buffer7[64] = {
	0x1F, 0x64, 0x00, 0x00, 0x01, 0x33, 0x33, 0x77, 
    0x88, 0x08, 0xc8, 0xb8, 0xb3, 0xb2, 0xbf, 0xa7, 0xb7, 0xc8,
    0x89, 0x0a, 0x00, 0x00, 0xFF, 0xFC, 0xF8, 
    0x00, 0x1F, 0xFF, 0xF1, 0xF0, 0x25
};
/*
unsigned char buffer7[64] = {
	0x29, 0x64, 0x00, 0x00, 0x01, 0x33, 0x33, 0x77, 
    0x88, 0x50, 0x65, 0x70, 0x73, 0x20, 0x73, 0x67, 
    0x64, 0x61, 0x73,
    0x89, 0x0a, 0x00, 0x00, 0xFF, 0xFC, 0xF8, 
    0x00, 0x1F, 0xFF, 0xF1, 0xF0, 0x25
};
*/

void main()
{
	volatile unsigned int i = 0;
    volatile unsigned int j = 0;
	// System Initialization
	StopWatchdog();               // Stop watchdog timer.
	ConfigPorts();                // Configure I/O ports.
    
    P5SEL |= BIT4 + BIT5;                       // Port select XT1

	LFXT_Start(XT1DRIVE_3, XCAP_3);
    Init_FLL_Settle(8000, 244);
    SFRIE1 |= OFIE;
    
    GT_config();
    LED_config();
    LED1_set();
    

    _BIS_SR(GIE);

    volatile unsigned char ii = 20;
    
    while (ii --) {
        LED1_toggle();
        __delay_cycles(500000);
    }
    
    while (1) {  
        
        combin(buffer1);
        memcombination(sum_buffer, buffer1);
        combin(buffer2);
        memcombination(sum_buffer, buffer2);
        combin(buffer3);
        memcombination(sum_buffer, buffer3);
        combin(buffer4);
        memcombination(sum_buffer, buffer4);
        combin(buffer5);
        memcombination(sum_buffer, buffer5);
        combin(buffer6);
        memcombination(sum_buffer, buffer6);
        combin(buffer7);
        memcombination(sum_buffer, buffer7);
        
	    handl_CN(sum_buffer);
        
        Clearn();

        Display_fan( mydata0, 0, 0, 
            70000ul, 4000ul, 4, 80000ul, 7500ul, 2, 0 ,1, 101);
	
        __delay_cycles(5000000);
    }
}

#pragma vector=TIMER1_A0_VECTOR
__interrupt void TIMER1_A0_ISR(void)
{
	if (gTimer == 0)     // Timer started by SetTimer() expired?
	{
		TA1CTL &= ~(MC1 + MC0);  // Stop timer.
		TA1CCTL0 &= ~CCIE;       // Disable timer interrupt.
		
		// Disable all interrupts and wake from sleep on exit.
		__bic_SR_register_on_exit(LPM3_bits);
	}
	else   // Timer not yet expired.
	{
		gTimer -= 1;
	}
}

/*******************************************************************************
* Function Name  : Buffercmp
* Description    : Compares two buffers.
* Input          : - pBuffer1, pBuffer2: buffers to be compared.
*                : - BufferLength: buffer's length
* Output         : None
* Return         : PASSED: pBuffer1 identical to pBuffer2
*                  FAILED: pBuffer1 differs from pBuffer2
*******************************************************************************/
TestStatus Buffercmp(unsigned char* pBuffer1, unsigned char* pBuffer2, unsigned int BufferLength)
{
	while(BufferLength--)
  	{
    	if(*pBuffer1 != *pBuffer2)
    	{
      		return FAILED;
    	}
    
    	pBuffer1++;
    	pBuffer2++;
  	}

  return PASSED;  
}
